<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    /* word-wrap: break-word;
      word-break: break-all; */
  </style>
</head>

<body>
  <a download="table导出Excel" id="excelOut" href="#">table导出Excel</a>
</body>
<script>
  "use strict"; {
    let arr = [{
      "_add_time": 1606379981,
      "status": "wait_pay",
      "status_easy": 0,
      "status_name": "已发货",
      "user_id": "12345678",
      "nickname": "李白",
      "user_avatar": "???",
      "inviter_uid": "???",
      "shop_id": "154614578",
      "shop_name": "好店铺",
      "agent_id": "778477777",
      "agent_name": "好人的吧",
      "delivery_object_type": 0,
      "delivery_object_id": "151555555",
      "type": 0,
      "order_no": "DD202009190942001234567800123456",
      "goods_amount": 2000,
      "total_amount": 234300,
      "delivery_amount": 1000,
      "should_payment_amount": 24741,
      "payment_amount": 13344,
      "need_payment_amount": 1477545,
      "weight": 12,
      "buyer_remark": "这个店家好人的",
      "flag_content": "好家伙",
      "payment_info": {
        "channel_trx_no": "dngiengoqo1519841205652010",
        "payment_methodId": 1
      },
      "goods_sku_list": {
        "_id": "1784454qda2degg",
        "shop_id": "744412222222",
        "shop_name": "同仁堂",
        "category_id": "12344fd",
        "category_name": "美食-三只松鼠",
        "goods_id": "123daffd",
        "goods_name": "俩只松鼠",
        "image": "???",
        "sku_name": "没有松鼠",
        "price": 14545,
        "market_price": 123.25,
        "stock": 2555,
        "weight": 2.2,
        "real_sales": 123,
        "update_time": 1606379981,
        "vip_price_rules": [],
        "is_not_vip_price": false,
        "buy_num": 124,
        "total_price": 123,
        "should_payment_amount": 120,
      },
      "delivery_detail": {
        "position": {
          "province": { "code": "12", "name": "天津市" },
          "city": { "code": "1201", "name": "市辖区" },
          "area": { "code": "120101", "name": "和平区" },
          "address": "天津市市辖区和平区怡丰城12区"
        },
        "formatted_address": "天津市市辖区和平区怡丰城12区",
        "delivery_type": 1,
        "id_card_no": "45092219970513049X",
        "receiver_name": "一封",
        "receiver_mobile": "16605818491",
        "send_name": "jk",
        "send_moblie": "13878022726",
        "kuaidi_name": "顺丰",
        "kuaidi_no": "YT4801838941022"
      },
      "enable_delivery": 1,
      "auto_confirm_received_time": 1606379981,
      "auto_cancel_time": 1606379981,
      "update_time": 1606379981,
      "is_not_refund": true,
      "is_pay": true,
      "is_pay_time": 1606379981,
      "is_cancel": false,
      "is_cancel_time": 1606379981,
      "is_send": true,
      "is_send_time": 1606379981,
      "is_apply_refund": true,
      "is_apply_refund_time": 1606379981,
      "is_refund": false,
      "is_refund_time": 1606379981,
      "is_confirm_receipt": true,
      "is_confirm_receipt_time": 1606379981,
      "is_comment": true,
      "is_comment_time": 1606379981,
      "is_complete": true,
      "is_complete_time": 1606379981,
      "is_close": false,
      "is_close_time": 1606379981
    }, {
      "_add_time": 1606379981,
      "status": "wait_pay",
      "status_easy": 0,
      "status_name": "已发货",
      "user_id": "12345678",
      "nickname": "李白",
      "user_avatar": "???",
      "inviter_uid": "???",
      "shop_id": "154614578",
      "shop_name": "好店铺",
      "agent_id": "778477777",
      "agent_name": "好人的吧",
      "delivery_object_type": 0,
      "delivery_object_id": "151555555",
      "type": 0,
      "order_no": "DD202009190942001234567800123456",
      "goods_amount": 2000,
      "total_amount": 234300,
      "delivery_amount": 1000,
      "should_payment_amount": 24741,
      "payment_amount": 13344,
      "is_refund_time": 1606379981,
      "is_confirm_receipt": true,
      "is_confirm_receipt_time": 1606379981,
      "is_comment": true,
      "is_comment_time": 1606379981,
      "is_complete": true,
      "is_complete_time": 1606379981,
      "is_close": false,
      "is_close_time": 1606379981
    }];
    let headers = {
      "_add_time": "本条记录添加时间",
      "status": "状态",
      "status_easy": "订单简易状态",
      "status_name": "订单状态名称",
      "user_id": "订单购买者用户id",
      "nickname": "订单购买者昵称",
      "user_avatar": "订单购买者的头像地址",
      "inviter_uid": "邀请人uid",
      "shop_id": "归属店铺id",
      "shop_name": "所属店铺名称",
      "agent_id": "所属代理商id",
      "agent_name": "所属代理商名称",
      "delivery_object_type": "该订单由谁来发货",
      "delivery_object_id": "店铺id",
      "type": "订单类型",
      "order_no": "订单号",
      "goods_amount": "商品总金额",
      "total_amount": "订单总金额",
      "delivery_amount": "运费",
      "should_payment_amount": "订单应付金额",
      "payment_amount": "订单实付金额",
      "need_payment_amount": "订单还需要付款的金额",
      "weight": "订单重量",
      "buyer_remark": "买家备注",
      "flag_content": "商家备注信息",
      "merchant_info": "商家信息",
      "payment_info": "支付信息",
      "goods_sku_list": "商品sku列表",
      "delivery_detail": "配送详情",
      "enable_delivery": "是否可发货",
      "auto_confirm_received_time": "订单自动确认收货时间",
      "auto_cancel_time": "订单自动取消时间",
      "update_time": "订单最后更新时间",
      "is_not_refund": "强制不可退款",
      "is_pay": "是否已付款",
      "is_pay_time": "付款时间",
      "is_cancel": "是否已取消",
      "is_cancel_time": "取消时间",
      "is_send": "是否已发货",
      "is_send_time": "发货时间",
      "is_apply_refund": "是否在申请退款",
      "is_apply_refund_time": "申请退款时间",
      "is_refund": "是否有退款",
      "is_refund_time": "商家同意退款时间",
      "is_confirm_receipt": "是否已确认收货",
      "is_confirm_receipt_time": "用户确认收货时间",
      "is_comment": "是否已评论",
      "is_comment_time": "评论时间",
      "is_complete": "是否已完成",
      "is_complete_time": "已完成时间",
      "is_close": "是否已关闭",
      "is_close_time": "关闭时间"
    };
    // 判断状态
    const judge_status = (key, item) => {
      switch (item[key]) {
        case "wait_pay":
          tr += `<td>待付款</td>`;
          break;
        case "is_cancel":
          tr += `<td>已取消(订单之前未付款)</td>`;
          break;
        case "is_pay":
          tr += `<td>已付款(待发货)</td>`;
          break;
        case "is_ready_send":
          tr += `<td>准备发货中(待发货)</td>`;
          break;
        case "is_send_all":
          tr += `<td>已全部发货(待收货)</td>`;
          break;
        case "is_send_part":
          tr += `<td>已部分发货(待收货)</td>`;
          break;
        case "is_apply_refund":
          tr += `<td>申请退款中(售后)</td>`;
          break;
        case "is_return_goods":
          tr += `<td>在退货中(买家发的退货仓库还未收到)</td>`;
          break;
        case "is_close":
          tr += `<td>已关闭(已全额退款)</td>`;
          break;
        case "is_refund_part":
          tr += `<td>已部分退款</td>`;
          break;
        case "is_confirm_receipt":
          tr += `<td>已确认收货(待评价)</td>`;
          break;
        case "is_comment":
          tr += `<td>已评论(已完成)</td>`;
          break;
        default:
          tr += `<td>已完成(该状态下不可退款)</td>`;
          break;
      };
    };
    // 订单简易状态
    const judge_status_easy = (key, item) => {
      switch (item[key]) {
        case -1:
          tr += `<td>待付款</td>`;
          break;
        case 0:
          tr += `<td>已取消(订单之前未付款)</td>`;
          break;
        case 1:
          tr += `<td>已付款(待发货)</td>`;
          break;
        case 2:
          tr += `<td>准备发货中(待发货)</td>`;
          break;
        case 3:
          tr += `<td>已全部发货(待收货)</td>`;
          break;
        case 4:
          tr += `<td>已部分发货(待收货)</td>`;
          break;
        case 5:
          tr += `<td>申请退款中(售后)</td>`;
          break;
        case 6:
          tr += `<td>在退货中(买家发的退货仓库还未收到)</td>`;
          break;
        case 7:
          tr += `<td>已关闭(已全额退款)</td>`;
          break;
        default: break;
      };
    };
    // 判断布尔值的
    const judge_boolean = (key, item) => {
      switch (key) {
        case "enable_delivery":
          tr += `<td>${item[key] ? "不可发货" : "可发货"}</td>`;
          break;
        case "is_not_refund":
          tr += `<td>${item[key] ? "可强制退款" : "不可强制退款"}</td>`;
          break;
        case "is_pay":
          tr += `<td>${item[key] ? "已付款" : "未付款"}</td>`;
          break;
        case "is_cancel":
          tr += `<td>${item[key] ? "已取消" : "未取消"}</td>`;
          break;
        case "is_send":
          tr += `<td>${item[key] ? "已发货" : "未发货"}</td>`;
          break;
        case "is_apply_refund":
          tr += `<td>${item[key] ? "申请退款" : "未申请退款"}</td>`;
          break;
        case "is_refund":
          tr += `<td>${item[key] ? "已确退款" : "未退款"}</td>`;
          break;
        case "is_confirm_receipt":
          tr += `<td>${item[key] ? "已确认收货" : "未确认收货"}</td>`;
          break;
        case "is_comment":
          tr += `<td>${item[key] ? "已评论" : "未评论"}</td>`;
          break;
        case "is_complete":
          tr += `<td>${item[key] ? "已完成" : "未完成"}</td>`;
          break;
        case "is_close":
          tr += `<td>${item[key] ? "已关闭" : "未关闭"}</td>`;
          break;
        default: break;
      };
    };
    // 判断支付类型
    const judge_payment_info = (key, item) => {
      let str = "";
      switch (item[key].payment_methodId) {
        case 1:
          str = "支付宝";
          break;
        case 2:
          str = "微信";
          break;
        case 3:
          str = "银行卡";
          break;
        case 4:
          str = "现金";
          break;
        case 5:
          str = "线下支付";
          break;
        default:
          str = "无需支付";
          break;
      };
      tr += `<td>第三方支付单号:${item[key]?.channel_trx_no}<br/>支付方式:${str}</td>`;
    };
    // 判断sku_list列表
    const judge_goods_sku_list = (key, item) => {
      item[key]._id &&= `商品sku id: ${item[key]._id}<br />`;
      item[key].shop_id &&= `所属店铺id: ${item[key].shop_id}<br />`;
      item[key].shop_name &&= `所属店铺名称: ${item[key].shop_name}<br />`;
      item[key].category_id &&= `分类 id: ${item[key].category_id}<br />`;
      item[key].category_name &&= `类别名称-全路径: ${item[key].category_name}<br />`;
      item[key].goods_id &&= `商品ID: ${item[key].goods_id}<br />`;
      item[key].goods_name &&= `商品名称: ${item[key].goods_name}<br />`;
      item[key].image &&= `sku图片: ${item[key].image}<br />`;
      item[key].sku_name &&= `sku名称: ${item[key].sku_name}<br />`;
      item[key].price &&= `价格/售价: ${item[key].price}<br />`;
      item[key].market_price &&= `市场价: ${item[key].market_price}<br />`;
      item[key].stock &&= `当前库存数量: ${item[key].stock}<br />`;
      item[key].weight &&= `重量: ${item[key].weight}<br />`;
      item[key].real_sales &&= `该规格实际销量: ${item[key].real_sales}<br />`;
      item[key].update_time &&= `修改时间: ${item[key].update_time}<br />`;
      item[key].vip_price_rules &&= `会员价规则: ${item[key].vip_price_rules}<br />`;
      item[key].is_not_vip_price = item[key].is_not_vip_price ? "禁用会员价<br />" : "不禁用会员价<br />";
      item[key].buy_num &&= `该sku购买的数量: ${item[key].buy_num}<br />`;
      item[key].total_price &&= `该sku合计的原价金额: ${item[key].total_price}<br />`;
      item[key].should_payment_amount &&= `该sku合计的应付金额: ${item[key].should_payment_amount}`;
      tr += `<td>${Object.values(item[key]).join("")}</td>`;
    };
    const judge_delivery_detail = (key, item) => {
      item[key].position &&= `地址信息: ${item[key].position.address}<br />`;
      item[key].formatted_address &&= `完整地址信息: ${item[key].formatted_address}<br />`;
      switch (item[key].delivery_type) {
        case 1:
          item[key].delivery_type = `配送方式: 物流配送<br />`;
          break;
        case 2:
          item[key].delivery_type = `配送方式: 同城限时达<br />`;
          break;
        case 3:
          item[key].delivery_type = `配送方式:到店自提<br />`;
          break;
        case 4:
          item[key].delivery_type = `配送方式: 门店交易<br />`;
          break;
        case 5:
          item[key].delivery_type = `配送方式: 无需物流<br />`;
          break;
        default: break;
      };
      item[key].delivery_type &&= `配送方式: ${item[key].delivery_type}<br />`;
      item[key].id_card_no &&= `收货人身份证: ${item[key].id_card_no}<br />`;
      item[key].receiver_name &&= `收货人姓名: ${item[key].receiver_name}<br />`;
      item[key].receiver_mobile &&= `收货人电话: ${item[key].receiver_mobile}<br />`;
      item[key].send_name &&= `发货人信息: ${item[key].send_name}<br />`;
      item[key].send_moblie &&= `发货人电话: ${item[key].send_moblie}<br />`;
      item[key].kuaidi_name &&= `快递公司: ${item[key].kuaidi_name}<br />`;
      item[key].kuaidi_no &&= `快递单号: ${item[key].kuaidi_no}`;
      tr += `<td>${Object.values(item[key]).join("")}</td>`;
    };
    const judge_merchant_info = (key, item) => {
      item[key].coupon_discount_info &&= `优惠券信息: ${item[key].coupon_discount_info}<br />`;
      item[key].coupon_code_discount_info &&= `码券信息: ${item[key].coupon_code_discount_info}<br />`;
      item[key].used_account_balance &&= `抵扣订单使用的 - 余额数量 : ${item[key].used_account_balance}<br />`;
      item[key].used_account_points &&= `抵扣订单使用的 - 积分数量: ${item[key].used_account_points}<br />`;
      item[key].used_vip_id &&= `抵扣订单使用的 - vip id: ${item[key].used_vip_id}<br />`;
      item[key].used_coupon_id &&= `抵扣订单使用的 - 优惠券id: ${item[key].used_coupon_id}<br />`;
      item[key].delivery_used_points &&= `抵扣运费使用 - 积分: ${item[key].delivery_used_points}<br />`;
      item[key].points_discount_delivery_amount &&= `运费抵扣金额 - 积分抵扣的运费金额: ${item[key].points_discount_delivery_amount}<br />`;
      item[key].balance_discount_delivery_amount &&= `运费抵扣金额 - 余额抵扣的运费金额: ${item[key].balance_discount_delivery_amount}<br />`;
      item[key].vip_discount_amount &&= `订单抵扣金额 - vip等级抵扣的订单金额 : ${item[key].vip_discount_amount}<br />`;
      item[key].points_discount_amount &&= `订单抵扣金额 - 使用积分抵扣的金额 : ${item[key].points_discount_amount}<br />`;
      item[key].balance_discount_amount &&= `订单抵扣金额 - 余额抵扣的金额  : ${item[key].balance_discount_amount}<br />`;
      item[key].merchant_discount_amount &&= `订单抵扣金额 - 商家改价优惠金额: ${item[key].merchant_discount_amount}<br />`;
      item[key].coupon_discount_amount &&= `订单抵扣金额 - 优惠券优惠金额 : ${item[key].coupon_discount_amount}<br />`;
      item[key].promotion_discount_amount &&= `订单抵扣金额 - 满减满折金额 : ${item[key].promotion_discount_amount}<br />`;
      item[key].coupon_code_discount_amount &&= `订单抵扣金额 - 码券优惠金额: ${item[key].coupon_code_discount_amount}<br />`;
      item[key].nynj_discount_amount &&= `订单抵扣金额 - N元N件抵扣金额: ${item[key].nynj_discount_amount}`;
      tr += `<td>${Object.values(item[key]).join("")}</td>`;
    };

    let [th, tr] = ["", "<tr>"];
    for (let value of Object.values(headers)) {
      th += `<th>${value}</th>`;
    };
    for (let item of arr.values()) {
      let headers_boolers = Object.keys(headers).some((value, index, arr) => {
        return item.hasOwnProperty(value);
      });
      // 如果头部的字段在每一项中都找不到，那就没有必要处理这一项的数据了。
      if (!headers_boolers) continue;
      // 处理每一项中的字段内容
      for (let key of Object.keys(headers)) {
        if (!item.hasOwnProperty(key)) {
          tr += `<td></td>`;
          continue;
        };
        if (key.lastIndexOf(`_time`) != -1 && item[key]) {
          tr += `<td>${new Date(item[key] * 1000).toLocaleString()}</td>`;
          continue;
        };
        // 判断状态
        if (key === "status" && item[key]) {
          judge_status(key, item);
          continue;
        };
        // 订单简易状态
        if (key == "status_easy") {
          judge_status_easy(key, item);
          continue;
        };
        // 判断支付类型
        if (key == "payment_info" && item[key]) {
          judge_payment_info(key, item);
          continue;
        };
        // 判断sku列表
        if (key == "goods_sku_list" && item[key]) {
          judge_goods_sku_list(key, item);
          continue;
        };
        // 判断地址信息
        if (key == "delivery_detail" && item[key]) {
          judge_delivery_detail(key, item);
          continue;
        };
        // 判断merchant_info
        if (key == "merchant_info" && item[key]) {
          judge_merchant_info(key, item);
          continue;
        };
        // 判断布尔值的
        if (key.indexOf(`is_`) != -1 || key == "enable_delivery") {
          judge_boolean(key, item);
          continue;
        };
        // 处理上面都不匹配的数据了
        tr += `<td>${item[key]}</td>`;
      };
      tr += `</tr>`;
    };

    let style = `*{padding:0;margin:0;box-sizing:border-box;}table{border-collapse:collapse;color:#606266;}th,td{border:1px solid #606266;padding:4px 8px;}thead{background-color:#eef1f6;letter-spacing:2px;}tbody>tr>td{text-align:left;}`;

    let str = `<!DOCTYPE html> <html lang="zh-CN"><head><style type="text/css">${style}</style></head><body><table><thead><tr>${th}</tr></thead><tbody>${tr}</tbody></table></body></html>`;
    console.log(str);

    document.addEventListener("DOMContentLoaded", () => {
      document.getElementById("excelOut").href = `data:application/vnd.ms-excel;base64,${window.btoa(unescape(encodeURIComponent(str)))}`;
    });

    /* 
    document.addEventListener("DOMContentLoaded", async () => {
      var kk = document.createElement("table");
      kk.innerHTML = `<thead><tr>${th}</tr></thead><tbody>${tr}</tbody>`;
      document.body.insertBefore(kk, document.body.firstChild);
    });
    */
  };
</script>

</html>